<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form ref="form" label-width="80px">
            <el-form-item label="处理结果:" class="w100p">
                <el-select v-model="defaulthandindex" width="100%" @change="defaulthandmet">
                    <el-option v-for="(deaitem, deaindex) in defaulthandinglist" :key="deaindex" :value="deaindex"
                        :label="deaitem" />
                </el-select>
            </el-form-item>
            <el-form-item label="处理意见:" class="w100p">
                <el-autocomplete type="textarea" style="width: 100%" v-model="ruleForm.disposeContent"
                    :fetch-suggestions="querySearch" placeholder="请输入处理意见" @select="handleSelect"></el-autocomplete>
            </el-form-item>
            <el-form-item label="温馨提示">
                <!-- 设为模板 -->
                <span>：你可以将处理意见设为模板，以便下次处理！</span>
                <el-button type="success" :disabled="!ruleForm.disposeContent" @click="setTemplate" style="margin: 0 auto"
                    plain>设为模板</el-button>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="clickqueren">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import alarmCategoryManagement from '@/api/monitoringAlarmManagement/alarmCategoryManagement.js';
import alarmHandling from '@/api/monitoringAlarmManagement/alarmHandling.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            defaulthandinglist: ["已处理完成", "忽略", "其他"],
            defaulthandindex: '',
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            templateList: []
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.handlingmmessage(this.data)
        }
    },

    mounted() { },
    methods: {
        // 处理
        async handlingmmessage(e) {
            this.ruleForm = { ...this.data };
            // TODO: 查询模板
            await this.queryTemplate(e);
            this.ruleForm = {};
        },
        // 查询模板
        async queryTemplate(e) {
            this.templateType = e.dataTypeName;
            let params = {
                url: this.$route.path,
                type: e.dataTypeName,
            };
            let res = await alarmCategoryManagement.queryTemplate(params);
            if (res.code === 1) {
                this.templateList = res.data;
                this.templateList.forEach((el) => {
                    el.value = el.content;
                });
            }
        },
        // TODO: 设为模板
        async setTemplate() {
            let params = {
                url: this.$route.path,
                account: sessionStorage.getItem("account"),
                type: this.templateType,
                content: this.ruleForm.disposeContent,
            };
            alarmCategoryManagement.setTemplate(params).then(res => {
                if (res.code == 1) {
                    this.$message.success(res.msg);
                }
                console.log(res)
            }).catch(err => {
                this.$message.info(err);
            })
        },
        // 排序模板
        async handleSelect(e) {
            await alarmCategoryManagement.sortTemplate(e.processContentId);
        },
        // 默认处理意见
        defaulthandmet(i) {
            if (i === 0) {
                this.ruleForm.disposeStatus = true;
            } else if (i === 1) {
                this.ruleForm.disposeStatus = false;
            } else if (i === 3) {
                this.ruleForm.disposeStatus = true;
            }
        },
        querySearch(queryString, cd) {
            cd(this.templateList);
        },
        //提交表单
        async clickqueren() {
            this.ruleForm.disposeTime = this.dateToFormat(new Date());
            if (this.defaulthandindex == 0) {
                this.ruleForm.disposeStatus = true;
            } else {
                this.ruleForm.disposeStatus = true;
            }
            if (!this.ruleForm.disposeContent) {
                this.ruleForm.disposeContent = "";
            }
            //单个处理
            console.log("单个处理");
            this.ruleForm.alarmMessageId = this.data.alarmMessageId
            alarmHandling.edit(this.ruleForm).then((res) => {
                if (res.code === 1) {
                    this.cancel();
                    this.$message.success("处理成功");
                } else {
                    this.$message.error("处理失败");
                }
            });
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  